﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class LabaRugi : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument RugiLaba;

        public LabaRugi()
        {
            DA = new DataAccess<object>();
            RugiLaba = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (! string.IsNullOrEmpty(txtTgl1.Text) && ! string.IsNullOrEmpty(txtTgl2.Text))
                 LoadReport(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")), DateTime.Parse(txtTgl2.Text, new CultureInfo("ID-id")));

           
        }

        public void LoadReport(DateTime tglStart, DateTime tglEnd)
        {
            StringBuilder sql = new StringBuilder();
            using (StreamReader sr = File.OpenText(Server.MapPath("~/sql/ReportRugiLaba.txt")))
            {
                String str = string.Empty;
                while ((str = sr.ReadLine()) != null)
                    sql.Append(str);
            }

            sql.Replace("@param1", tglStart.ToString("yyyy-MM-dd"));
            sql.Replace("@param2", tglEnd.ToString("yyyy-MM-dd"));
           

            var Source = DA.GetDataListDataSet(sql.ToString());
            string reportPath = Server.MapPath("~/Report/ReportRugiLaba.rpt");
            RugiLaba.Load(reportPath);
            RugiLaba.SetDataSource(Source.Tables[0]);
            SetupParameter(tglStart, tglEnd, RugiLaba);
            CrystalReportViewer1.ReportSource = RugiLaba;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        private void SetupParameter(DateTime tglStart, DateTime tglEnd, ReportDocument reportDocument)
        {
            var param = new ParameterDiscreteValue();
            param.Value = tglStart.ToString("dd-MM-yyyy");
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            reportDocument.DataDefinition.ParameterFields["tglawal"].ApplyCurrentValues(paramValue);
            var param2 = new ParameterDiscreteValue();
            param2.Value = tglEnd.ToString("dd-MM-yyyy");
            var paramValue2 = new ParameterValues();
            paramValue2.Add(param2);
            reportDocument.DataDefinition.ParameterFields["tglakhir"].ApplyCurrentValues(paramValue2);
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
           
        }


        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            RugiLaba.Clone();
            RugiLaba.Dispose();

        }
    }
}